package cc.admin.modules.graduate.service.impl;

import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.graduate.config.WebSocketServer;
import cc.admin.modules.graduate.entity.Forum;
import cc.admin.modules.graduate.entity.Topic;
import cc.admin.modules.graduate.mapper.ForumMapper;
import cc.admin.modules.graduate.service.IForumService;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 话题树
 * @Author: cc-admin
 * @Date: 2025-02-13
 * @Version: V1.0.0
 */
@Service
public class ForumServiceImpl extends ServiceImpl<ForumMapper, Forum> implements IForumService {

    @Autowired
    private WebSocketServer webSocketServer;

    @Override
    public void saveForum(Forum forum) {
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();

        Topic topic = forum.getTopic();
        forum.setTopicLabel(topic.getLabel());
        forum.setTopicValue(topic.getValue());
        if (forum.getUnnamedFlag() == true) {
            forum.setUnnamed(UuidUtils.getUUID());  //随机获取一串数字作为名字
        }else {
            forum.setUnnamed(user.getRealname());   //获取当前用户姓名
            forum.setAvatar(user.getAvatar());   //获取当前用户头像
        }
        String videoTring = String.join(",", forum.getViedoUrls());
        forum.setViedoUrlText(videoTring);
        this.save(forum);
        String forumJson = JSONObject.toJSONString(forum);
        webSocketServer.sendAll(forumJson);
    }
}
